package com.wavehi.web.homework.third;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author WaveHi
 * @Date 17/9/2021 下午 8:44
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Demonstrate the use of inner object
 */
@WebServlet("/innerObjectDemoServlet")
public class InnerObjectDemoServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //向域对象设置数据
        req.setAttribute("user", "Tom");
        req.getSession().setAttribute("age", "12");
        getServletConfig().getServletContext().setAttribute("email", "roeroe@163.com");

        //获取域对象的数据
        System.out.println("User : " + req.getAttribute("user"));
        System.out.println("Age : " + req.getSession().getAttribute("age"));
        System.out.println("Email : " + getServletContext().getAttribute("email"));

        //移除域对象指定数据
        req.removeAttribute("user");
        req.getSession().removeAttribute("age");
        getServletContext().removeAttribute("email");
        System.out.println(req.getAttribute("user"));
        System.out.println(req.getSession().getAttribute("age"));
        System.out.println(getServletContext().getAttribute("email"));

    }
}
